package com.example.volunteer.controller;

import com.example.volunteer.model.Activity;
import com.example.volunteer.service.ActivityService;
import com.example.volunteer.util.PageResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.sql.Date;

@RestController
@RequestMapping("/api/activities")
public class ActivityController {

    @Autowired
    private ActivityService activityService;

    @GetMapping
    public ResponseEntity<PageResult<Activity>> listActivities(
            @RequestParam(defaultValue = "1") int pageNum,
            @RequestParam(defaultValue = "10") int pageSize) {
        return ResponseEntity.ok(activityService.findActivitiesWithPage(pageNum, pageSize));
    }

    @PostMapping
    @PreAuthorize("hasRole('ADMIN')")
    public ResponseEntity<String> createActivity(@RequestBody Activity activity) {
        activityService.createActivity(activity);
        System.out.println("添加活动功能");
        return ResponseEntity.ok("活动创建成功");
    }

    @PutMapping
    @PreAuthorize("hasRole('ADMIN')")
    public ResponseEntity<String> updateActivity(@RequestBody Activity activity) {
        activityService.updateActivity(activity);
        return ResponseEntity.ok("活动更新成功");
    }

    @GetMapping("/id")
    @PreAuthorize("hasRole('ADMIN')")
    public ResponseEntity<Activity> findById(@RequestParam int id) {
        return ResponseEntity.ok(activityService.findById(id));
    }

    @DeleteMapping("/id")
    @PreAuthorize("hasRole('ADMIN')")
    public ResponseEntity<String> deleteActivity(@RequestParam int id) {
        activityService.delete(id);
        return ResponseEntity.ok("活动删除成功");
    }

    @GetMapping("/title")
    @PreAuthorize("hasAnyRole('ADMIN','VOLUNTEER')")
    public ResponseEntity<PageResult<Activity>> findByTitleWithPage(
            @RequestParam String title,
            @RequestParam(defaultValue = "1") int pageNum,
            @RequestParam(defaultValue = "10") int pageSize) {
        return ResponseEntity.ok(activityService.findByTitleWithPage(title, pageNum, pageSize));
    }

    @GetMapping("/start-time")
    @PreAuthorize("hasAnyRole('ADMIN','VOLUNTEER')")
    public ResponseEntity<PageResult<Activity>> findByStartTime(
            @RequestParam Date startTime,
            @RequestParam(defaultValue = "1") int pageNum,
            @RequestParam(defaultValue = "10") int pageSize) {
        return ResponseEntity.ok(activityService.findByStartTimeWithPage(startTime, pageNum, pageSize));
    }

    @GetMapping("/end-time")
    @PreAuthorize("hasAnyRole('ADMIN','VOLUNTEER')")
    public ResponseEntity<PageResult<Activity>> findByEndTime(
            @RequestParam Date endTime,
            @RequestParam(defaultValue = "1") int pageNum,
            @RequestParam(defaultValue = "10") int pageSize) {
        return ResponseEntity.ok(activityService.findByEndTimeWithPage(endTime, pageNum, pageSize));
    }

    @GetMapping("/address")
    @PreAuthorize("hasAnyRole('ADMIN','VOLUNTEER')")
    public ResponseEntity<PageResult<Activity>> findByAddress(
            @RequestParam String address,
            @RequestParam(defaultValue = "1") int pageNum,
            @RequestParam(defaultValue = "10") int pageSize) {
        return ResponseEntity.ok(activityService.findByAddressWithPage(address, pageNum, pageSize));
    }

    @GetMapping("/participant-limit")
    @PreAuthorize("hasAnyRole('ADMIN','VOLUNTEER')")
    public ResponseEntity<PageResult<Activity>> findByParticipantLimit(
            @RequestParam int participantLimit,
            @RequestParam(defaultValue = "1") int pageNum,
            @RequestParam(defaultValue = "10") int pageSize) {
        return ResponseEntity.ok(activityService.findByParticipantLimitWithPage(participantLimit, pageNum, pageSize));
    }

    @GetMapping("/created-time")
    @PreAuthorize("hasRole('ADMIN')")
    public ResponseEntity<PageResult<Activity>> findByCreatedTime(
            @RequestParam Date createdTime,
            @RequestParam(defaultValue = "1") int pageNum,
            @RequestParam(defaultValue = "10") int pageSize) {
        return ResponseEntity.ok(activityService.findByCreatedTimeWithPage(createdTime, pageNum, pageSize));
    }

    @GetMapping("/admin")
    @PreAuthorize("hasAnyRole('ADMIN','VOLUNTEER')")
    public ResponseEntity<PageResult<Activity>> findByAdminId(
            @RequestParam String adminId,
            @RequestParam(defaultValue = "1") int pageNum,
            @RequestParam(defaultValue = "10") int pageSize) {
        return ResponseEntity.ok(activityService.findByAdminIdWithPage(adminId, pageNum, pageSize));
    }

    @GetMapping("/status")
    @PreAuthorize("hasAnyRole('ADMIN','VOLUNTEER')")
    public ResponseEntity<PageResult<Activity>> findByStatus(
            @RequestParam String status,
            @RequestParam(defaultValue = "1") int pageNum,
            @RequestParam(defaultValue = "10") int pageSize) {
        return ResponseEntity.ok(activityService.findByStatusWithPage(status, pageNum, pageSize));
    }

    @PutMapping("/status")
    @PreAuthorize("hasRole('ADMIN')")
    public ResponseEntity<String> updateStatus(@RequestParam Integer id, @RequestParam String status) {
        activityService.updateActivityStatus(id, status);
        return ResponseEntity.ok("活动状态更新成功");
    }
}